import java.util.Arrays;
import java.util.Scanner;

public class HeapSort {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
        int len=in.nextInt();
        int[] arr=new int[len];
        for (int i=0; i<len; i++) {
            arr[i]=in.nextInt();
        }
        heapsort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void heapsort(int[] arr){
        for (int i=arr.length-1;i>0;i--) {
            createMaxHeap(arr,i);
            swap(arr,0,i);
        }
    }
    
    public static void createMaxHeap(int[] arr,int high){
        
        for (int i=(high-1)/2; i>=0; i--) {
            if (i*2+1<=high && arr[i]<arr[i*2+1]) {
                swap(arr,i,i*2+1);
            }
            if (i*2+2<=high && arr[i]<arr[i*2+2]) {
                swap(arr,i,i*2+2);
            }
        }
    
    }

    public static void swap(int arr[] ,int low ,int high){
        int tmp=arr[low];
        arr[low]=arr[high];
        arr[high]=tmp;
    }
}
